﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml;

namespace UploadBlobToAzure
{
    class field_validator
    {
       public bool testIfFieldEmpty(string fieldValue)
        {
            if (string.IsNullOrWhiteSpace(fieldValue))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool testIfFieldIsAlphaNumeric(string fieldValue)
       {
           if (testIfFieldEmpty(fieldValue))
            {
                return false;
            }

            return (fieldValue.ToCharArray().All(c => Char.IsLetter(c) || Char.IsNumber(c)));
       }

        public bool checkIfFileExists(string fileName)
        {
            if(File.Exists(fileName))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool testIfStorageAccountExistsInFile(string storageAccount,string fileName)
        {
            XmlDocument doc = new XmlDocument();
            if(checkIfFileExists(fileName))
            {
                doc.Load(fileName);
                XmlNodeList xmlnl = doc.SelectNodes("xml/configuration");
                foreach (XmlNode xml in xmlnl)
                {
                    XmlNode node = xml.SelectSingleNode("storageaccount");
                    if (node.InnerText.ToString().ToLowerInvariant() == storageAccount.ToLowerInvariant())
                    {
                        return true;
                    }
                }
                return false;
             }
            else
            {
                return false;
            }

        }
    }
}
